package com.hange.ymcc.web.controller;

import com.hange.ymcc.dto.AlipayNotifyDto;
import com.hange.ymcc.dto.PayApplyDto;
import com.hange.ymcc.result.JSONResult;
import com.hange.ymcc.service.IPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private IPayOrderService payOrderService;

    /**
     * 支付宝异步回调的接口
     * dto:
     * AlipayNotifyDto alipayNotifyDto: 原始方式发送的参数
     */
    @PostMapping("/notify")
    public String payNotify(AlipayNotifyDto alipayNotifyDto) {
        try {
            return payOrderService.payNotify(alipayNotifyDto);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 检查支付单是否已生成
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo) {

        return payOrderService.checkPayOrder(orderNo);

    }

    /**
     * 支付请求
     */
    @PostMapping("/apply")
    public JSONResult apply(@RequestBody PayApplyDto payApplyDto) {

        String payForm = payOrderService.apply(payApplyDto);

        return JSONResult.success(payForm);
    }


}